/**
 * @Author: LIANG YAN BIAO
 * @Date:   on 2020/3/13
 */

// import * as globalFilters from '@/filters/globals'
// import * as globalDiretives from '@/directives/globals'
import {
	mapGetters
} from 'vuex'
export default {
	install(Vue, options) {
		// 全局 mixin
		Vue.mixin({
			data() {
				return {
					staticImgUrl: 'https://toc.nanyuecloud.com/toc-inst-app/zqanswer/test/img/xmg/',
					baseImg: 'https://www.kanfoshan.cn/'
				}
			},
			computed: {
				// #ifdef H5
				...mapGetters(['env'])
				// #endif
			},
			methods: {
				setGoodsBrowse(goods_id, sku_id, category_id, category_id_1, category_id_2, category_id_3,
					site_id) {
					this.$Apis.setGoodsBrowse({
						goods_id,
						sku_id,
						category_id,
						category_id_1,
						category_id_2,
						category_id_3,
						site_id
					}).then(res => {
						// console.log("res=>", res)
					}).catch(err => {
						console.log('err=>', err)
					})
				},
				// 后退
				// back() {
				//     if (this.isNativeApp()) {
				//         sdk.exit()
				//     } else {
				//         this.$router.back(-1)
				//     }
				// },
				// 判断是否是原生环境
				isNativeApp() {
					return this.env.includes('NativeApp')
				},
				// 获取url上面指定的key
				getLocationKey(variable) {
					// let str = 'http://localhost:8089/?code=081rNoGa1aI4FB0XQmGa1WLrbb2rNoGA&state=STATE#/shopPages/goods/detail?goods_id=2543&is_id=2543&type=1'
					if (!location.href.includes('?')) return false
					var vars = location.href.split('?')[1].split('&')
					for (var i = 0; i < vars.length; i++) {
						var pair = vars[i].split('=')
						if (pair[0] == variable) {
							return pair[1]
						}
					}
					return false
				},
				// 获取url上面指定的key
				getLocationKeyUrl(variable, url) {
					if (!url.includes('?')) return false
					var vars = url.split('?')[1].split('&')
					for (var i = 0; i < vars.length; i++) {
						var pair = vars[i].split('=')
						if (pair[0] == variable) {
							return pair[1]
						}
					}
					return false
				},
				// 微信客户端：授权登录
				appWxAuthAction(appid, backurl = '') {
					// if (!location.origin.includes('toc.nanyuecloud.com')) {
					// 	console.info('===========')
					// 	console.info('授权域名：toc.nanyuecloud.com')
					// 	console.info('============')
					// 	return false
					// }
					let callbackUrl
					// 判断一下是否有backurl
					if (backurl) {
						callbackUrl = encodeURIComponent(backurl)
					} else {
						let callbackUrl = encodeURIComponent(WEB_URL)
						if (location.href.split('code=').length !== 1) {
							let href = WEB_URL
							href = href.match(/(\S*)\?code=/)[1] + href.match(/=STATE(\S*)/)[1]
							callbackUrl = encodeURIComponent(href)
						}
					}
					location.href =
                        `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid || APP_ID}&redirect_uri=${callbackUrl}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
				}
			}
		})

		// 扩展原型
		// Reflect.defineProperty(Vue.prototype, '$waiting', {
		//     value: waiting
		// })

		// //全局过滤器
		// Object.keys(globalFilters).forEach(key => {
		//     Vue.filter(key, globalFilters[key])
		// })

		// //全局指令
		// Object.keys(globalDiretives).forEach(key => {
		//     Vue.directive(key, globalDiretives[key])
		// })
	}
}
